package hu.diploma.web.server.restlet.resources;

import hu.diploma.common.resource.PersonResource;
import hu.diploma.common.service.PersonService;
import hu.diploma.common.vos.PersonVo;
import hu.diploma.core.util.ServiceProviderImpl;

import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

public class PersonResourceImpl extends ServerResource implements PersonResource {

	public PersonVo merge(PersonVo request) {
		// try {
		PersonService personService = ServiceProviderImpl.getServiceProvider().getPersonService();
		// PersonalDataService personalDataService =
		// SpringUtil.getPersonalDataService();
		// StructuredNameService structuredNameService =
		// SpringUtil.getStructuredNameService();
		// AddressService addressService = SpringUtil.getAddressService();
		// PersonVo person = personService.findByPhoneId(request.getPhoneId());
		//
		// if (person != null && request.getVersion() > person.getVersion()) {
		// return null;
		// } else {
		// PersonVo response = new PersonVo();
		// List<PersonalDataVo> dataVos = new ArrayList<PersonalDataVo>();
		// for (PersonalDataVo requestPersonalDataVo :
		// request.getPersonalDatas()) {
		// PersonalDataVo personalDataVo =
		// personalDataService.findByPhoneId(requestPersonalDataVo.getPhoneId());
		// if (requestPersonalDataVo.getVersion() > personalDataVo.getVersion())
		// {
		// personalDataService.merge(requestPersonalDataVo);
		// } else {
		// dataVos.add(personalDataVo);
		// }
		// }
		// if (dataVos.size() > 0) {
		// response.setPersonalDatas(dataVos);
		// }
		// StructuredNameVo structuredNameVo =
		// structuredNameService.findByPhoneId(request.getStructuredName().getPhoneId());
		//
		// if (request.getStructuredName().getVersion() >
		// structuredNameVo.getVersion()) {
		// structuredNameService.merge(structuredNameVo);
		// } else {
		// response.setStructuredName(structuredNameVo);
		// }
		// List<AddressVo> addresses = new ArrayList<AddressVo>();
		// for (AddressVo requestAddressVo : request.getAddresses()) {
		//
		// AddressVo addressVo =
		// addressService.findByPhoneId(requestAddressVo.getPhoneId());
		// if (requestAddressVo.getVersion() > addressVo.getVersion()) {
		// addressService.merge(requestAddressVo);
		// } else {
		//
		// }
		// }
		// if (addresses.size() > 0) {
		// request.setAddresses(addresses);
		// }
		// return response;
		// }
		//
		// } catch (Exception e) {
		//
		// e.printStackTrace();
		// return null;
		// }

		try {
			return personService.merge(request);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Put
	public PersonVo craete(PersonVo personVo) {
		PersonService personService = ServiceProviderImpl.getServiceProvider().getPersonService();
		try {

			return personService.save(personVo);

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
